#include "ObjWriter.hpp"
#include <iostream>
#include "common/logger.h"
#include "objw/objw.hpp"

namespace  AMCAX::Exchange {

    Dtk_ErrorStatus ObjWriter::WriteInit(Dtk_string dumpFile, const Dtk_string& outputFile)
    {
        return WriteInit(dumpFile, outputFile, 0);
    }

    Dtk_ErrorStatus ObjWriter::WriteInit(Dtk_string dumpFile, const Dtk_string& outputFile, int option)
    {
        m_outputFileName = outputFile;
        std::cout << "Output file is : " << m_outputFileName << std::endl;

        Objw::WriteOptions options;
	    options.m_UseRender = DTK_TRUE;
        DtkErrorStatus status = Objw::InitWrite( m_outputFileName, Dtk_string( "" ), options );

        if( status != dtkNoError )
        {
            std::cout << "error : " << dtkTypeError( status ).c_str() << std::endl;
            return status;
        }

        return dtkNoError;
    }

    Dtk_ErrorStatus ObjWriter::WriteEnd()
    {
        auto status = Objw::EndWrite();
        if( status != dtkNoError )
        {
            std::cout << "error : " << dtkTypeError( status ).c_str() << std::endl;
        }
        else
            std::cout << "=> " << m_outputFileName.c_str() << std::endl;

        return status;
    }

    void ObjWriter::WriteMesh(Dtk_MeshPtr& inMeshToWrite, Dtk_tab<Dtk_ID>& outIds)
    {
        auto status = Objw::WriteEntity( Dtk_EntityPtr::DtkDynamicCast(inMeshToWrite) );
        if (status != dtkNoError)
        {
            LOG_ERROR("WriteMesh failed with error {}", (int)status);
        }

    }

    void ObjWriter::WriteBody(Dtk_BodyPtr& inBody, Dtk_tab<Dtk_ID>& outIds)
    {
        auto status = Objw::WriteEntity(Dtk_EntityPtr::DtkDynamicCast(inBody) );
        if (status != dtkNoError)
        {
            LOG_ERROR("WriteMesh failed with error {}", (int)status);
        }
    }


}
